package com.lime49.lockcrypt;

import com.lime49.lockcrypt.ui.AbstractDialog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Prompts the user to enter an input delay (only accepts numbers)
 */
public class FrmPromptDelay extends AbstractDialog {
    private ILanguageParent owner;
    
    private JTextField txtInput;
    
    /**
     * Creates a new dialog to prompt the user to enter an input delay (only accepts numbers)
     * @param owner The ILanguageParent owner dialog
     */
    public FrmPromptDelay(ILanguageParent owner) {
        super(owner instanceof JDialog ? (JDialog)owner : null, owner.getLanguage().getString("INSERTDELAY"), true);
        this.owner = owner;
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction() {
            // cancel the dialog when the user presses escape
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        }; 
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
        JPanel content = new JPanel(new GridBagLayout());//new FlowLayout(GridLayout(0,1));
        this.setContentPane(content);
        //content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
        content.setBorder(Constants.EMPTYBORDER);
        GridBagConstraints c = new GridBagConstraints();
        //c.insets = Constants.GLOBALINSETS;
        int cury = 0;
            
        JLabel lblDelay = new JLabel(owner.getLanguage().getString("DELAYINMILLIS"));
        Utils.setgrid(c,cury,0,1,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.NORTHWEST,1);
        content.add(lblDelay,c);
        cury++;
        
        txtInput = new JTextField(5);
        Utils.setgrid(c,cury,0,1,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.NORTHWEST,1);
        content.add(txtInput,c);
        cury++;
        
        JPanel buttonBox = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        Utils.setgrid(c,cury,0,1,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.NORTHWEST,1);
        content.add(buttonBox,c);
        cury++;
        JButton btnOk = new JButton(owner.getLanguage().getString("OK"));
        buttonBox.add(btnOk);
        btnOk.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               if(validateInput()) {
                   result = JOptionPane.OK_OPTION;
                   dispose();
               }
           }
        });
        buttonBox.add(Box.createHorizontalStrut(5));
        JButton btnCancel = new JButton(owner.getLanguage().getString("CANCEL"));
        buttonBox.add(btnCancel);
        btnCancel.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               dispose();
           }
        });
        btnOk.setMinimumSize(Constants.MINSIZE);
        btnOk.setPreferredSize(Constants.PREFSIZE);
        btnOk.setMaximumSize(Constants.MAXSIZE);
        btnCancel.setMinimumSize(Constants.MINSIZE);
        btnCancel.setPreferredSize(Constants.PREFSIZE);
        btnCancel.setMaximumSize(Constants.MAXSIZE);
        
        c.weighty = 1;
        Utils.setgrid(c,cury,0,1,1,GridBagConstraints.BOTH,GridBagConstraints.NORTHWEST,1);
        content.add(Box.createVerticalGlue(),c);
        
        this.getRootPane().setDefaultButton(btnOk);
        this.setMinimumSize((new Dimension(200,90)));
        this.setPreferredSize((new Dimension(250,120)));
        this.setMaximumSize((new Dimension(300,200)));
        this.pack();
        this.setLocationRelativeTo(owner.getParentWindow());
        this.setVisible(true);
    }
    
    /**
     * Checks that the delay entered contains only numbers
     * @return True if the input is valid, otherwise false
     */
    private boolean validateInput() {
        boolean valid = true;
        String inp = txtInput.getText().trim();
        if(inp == null || inp.length() == 0) {
            valid = false;
        } else {
            for(int i=0;i<inp.length();i++) {
                if(!Character.isDigit(inp.charAt(i))) {
                    JOptionPane.showMessageDialog(thisDialog, owner.getLanguage().getString("ERR_DELAYNOTNUMBER"), owner.getLanguage().getString("ERROR"), JOptionPane.ERROR_MESSAGE);
                    valid = false;
                    txtInput.selectAll();
                    break;
                }
            }
        }
        return valid;
    }
    
    /**
     * Returns the delay entered
     * @return The delay entered
     */
    public int getDelay() {
        int ret = 1000;
        try {
            ret = Integer.parseInt(txtInput.getText());
        } catch(Exception ignore) {}
        return ret;
    }
}
